<template>
  <div class="page-wrap">
    <el-card shadow="never">
      <div class="toolbar">
        <el-input v-model="filters.search" placeholder="搜索名称/编码" clearable class="w-300" />
        <el-select v-model="filters.status" placeholder="状态" clearable class="w-160">
          <el-option label="正常" value="正常" />
          <el-option label="预警" value="预警" />
          <el-option label="缺货" value="缺货" />
        </el-select>
      </div>

      <el-table :data="dataList" border stripe height="560px">
        <el-table-column prop="code" label="编码" width="120" />
        <el-table-column prop="name" label="名称" min-width="160" />
        <el-table-column prop="warehouse" label="仓库" width="120" />
        <el-table-column prop="quantity" label="库存" width="100" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="statusType(row)">{{ statusText(row) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="openInventory(row)">盘点</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="invVisible" title="库存盘点" width="460px">
      <el-form :model="invForm" label-width="90px">
        <el-form-item label="商品">
          <span>{{ currentRow?.name }}（{{ currentRow?.code }}）</span>
        </el-form-item>
        <el-form-item label="当前库存">
          <span>{{ currentRow?.quantity }}</span>
        </el-form-item>
        <el-form-item label="实盘数量">
          <el-input-number v-model="invForm.actualQuantity" :min="0" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="invForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="invVisible=false">取消</el-button>
        <el-button type="primary" @click="onInventory">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, computed, onMounted } from 'vue'
import { useStockStore } from '@/stores/stock.js'

const store = useStockStore()
const filters = reactive({ search: '', status: '' })
const dataList = computed(() => store.getFilteredStock(filters))

onMounted(() => {
  if (!store.stockList.length) store.initializeStock()
})

function statusText(row){ return store.getItemStatus(row) }
function statusType(row){ const s = statusText(row); return s==='正常'?'success':(s==='预警'?'warning':'danger') }

const invVisible = ref(false)
const currentRow = ref(null)
const invForm = reactive({ actualQuantity: 0, remark: '' })

function openInventory(row){
  currentRow.value = row
  invForm.actualQuantity = row.quantity
  invForm.remark = ''
  invVisible.value = true
}
function onInventory(){
  store.inventoryStock({ productId: currentRow.value.id, actualQuantity: invForm.actualQuantity, operator: '系统', date: new Date().toISOString(), remark: invForm.remark })
  invVisible.value = false
}
</script>

<style scoped>
.page-wrap { padding: 16px; }
.toolbar { display: flex; gap: 12px; margin-bottom: 12px; align-items: center; }
.w-300 { width: 300px; }
.w-160 { width: 160px; }
</style>


